﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "protocol/serial.h"

#define MAX_SELECT_BOX 4
void MainWindow::PlotInit(){
    plotMan = new PlotManage(ui->plot);
    InitCheckBoxProp();
    /* Remove everything from the plot */
    ui->plot->clearItems();

    /* Background for the plot area */
    ui->plot->setBackground (gui_colors[0]);

    /* Used for higher performance (see QCustomPlot real time example) */
    ui->plot->setNotAntialiasedElements (QCP::aeAll);
    QFont font;
    font.setStyleStrategy (QFont::NoAntialias);
    ui->plot->legend->setFont (font);
    ui->plot->legend->setTextColor(Qt::white);
    /** See QCustomPlot examples / styled demo **/
    /* X Axis: Style */
    ui->plot->xAxis->grid()->setPen (QPen(gui_colors[2], 1, Qt::DotLine));
    ui->plot->xAxis->grid()->setSubGridPen (QPen(gui_colors[1], 1, Qt::DotLine));
    ui->plot->xAxis->grid()->setSubGridVisible (true);
    ui->plot->xAxis->setBasePen (QPen (gui_colors[2]));
    ui->plot->xAxis->setTickPen (QPen (gui_colors[2]));
    ui->plot->xAxis->setSubTickPen (QPen (gui_colors[2]));
    ui->plot->xAxis->setUpperEnding (QCPLineEnding::esSpikeArrow);
    ui->plot->xAxis->setTickLabelColor (gui_colors[2]);
    ui->plot->xAxis->setTickLabelFont (font);
    /* Range */
    ui->plot->xAxis->setRange (0, ui->spinPoints->value());

    /* Y Axis */
    ui->plot->yAxis->grid()->setPen (QPen(gui_colors[2], 1, Qt::DotLine));
    ui->plot->yAxis->grid()->setSubGridPen (QPen(gui_colors[1], 1, Qt::DotLine));
    ui->plot->yAxis->grid()->setSubGridVisible (true);
    ui->plot->yAxis->setBasePen (QPen (gui_colors[2]));
    ui->plot->yAxis->setTickPen (QPen (gui_colors[2]));
    ui->plot->yAxis->setSubTickPen (QPen (gui_colors[2]));
    ui->plot->yAxis->setUpperEnding (QCPLineEnding::esSpikeArrow);
    ui->plot->yAxis->setTickLabelColor (gui_colors[2]);
    ui->plot->yAxis->setTickLabelFont (font);
    /* Range */
    //ui->plot->yAxis->setRange (ui->spinAxesMin->value(), ui->spinAxesMax->value());
    /* User can change Y axis tick step with a spin box */
    //ui->plot->yAxis->setAutoTickStep (false);
    //ui->plot->yAxis->(ui->spinYStep->value());

    /* User interactions Drag and Zoom are allowed only on X axis, Y is fixed manually by UI control */
    ui->plot->setInteraction (QCP::iRangeDrag, true);
    //ui->plot->setInteraction (QCP::iRangeZoom, true);
    ui->plot->setInteraction (QCP::iSelectPlottables, true);
    ui->plot->setInteraction (QCP::iSelectLegend, true);
    ui->plot->axisRect()->setRangeDrag (Qt::Horizontal);
    ui->plot->axisRect()->setRangeZoom (Qt::Horizontal);

    /* Legend */
    QFont legendFont;
    legendFont.setPointSize (9);
    ui->plot->legend->setVisible (true);
    ui->plot->legend->setFont (legendFont);
    ui->plot->legend->setBrush (gui_colors[3]);
    ui->plot->legend->setBorderPen (gui_colors[2]);
    /* By default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement */
    ui->plot->axisRect()->insetLayout()->setInsetAlignment (0, Qt::AlignTop|Qt::AlignRight);

    /* Wheel over plot when plotting */
    connect (ui->plot, &QCustomPlot::mouseWheel, this, &MainWindow::OnMouse_wheel_in_plot);

    /* Slot for printing coordinates */
    connect (ui->plot, &QCustomPlot::mouseMove, this, &MainWindow::onMouseMoveInPlot);

    connect (ui->plot, &QCustomPlot::mouseDoubleClick, this, &MainWindow::onMouseDoubleClick);

    /* Channel selection */
    connect (ui->plot, &QCustomPlot::selectionChangedByUser, this, &MainWindow::channel_selection);
    connect (ui->plot, &QCustomPlot::legendDoubleClick, this, &MainWindow::legend_double_click);
}

void MainWindow::InitCheckBoxProp(){
    ui->checkBox_phase_curr->setProperty("Num", 3);
    QList<QVariant> list;
    list.append(PHASEA_CURR_IDX);
    list.append(PHASEB_CURR_IDX);
    list.append(PHASEC_CURR_IDX);
    ui->checkBox_phase_curr->setProperty("Id", list);

    ui->checkBox_phase_vol->setProperty("Num", 3);
    list.clear();
    list.append(PHASEA_VOL_IDX);
    list.append(PHASEB_VOL_IDX);
    list.append(PHASEC_VOL_IDX);
    ui->checkBox_phase_vol->setProperty("Id", list);

    ui->checkBox_in_torque->setProperty("Num", 1);
    ui->checkBox_in_torque->setProperty("Id", INPUT_TORQUE_IDX);

    ui->checkBox_out_torque->setProperty("Num", 1);
    ui->checkBox_out_torque->setProperty("Id", OUTPUT_TORQUE_IDX);

    ui->checkBox_in_vel->setProperty("Num", 1);
    ui->checkBox_in_vel->setProperty("Id", INPUT_VELOCITY_IDX);

    ui->checkBox_mot_vel->setProperty("Num", 1);
    ui->checkBox_mot_vel->setProperty("Id", ENCODER_VELOCITY_IDX);

    ui->checkBox_mot_angle->setProperty("Num", 1);
    ui->checkBox_mot_angle->setProperty("Id", ENCODER_ANGLE_IDX);

    ui->checkBox_dq_current->setProperty("Num", 2);
    list.clear();
    list.append(D_AXIS_CURR_IDX);
    list.append(Q_AXIS_CURR_IDX);
    ui->checkBox_dq_current->setProperty("Id", list);

    ui->checkBox_dq_curr_target->setProperty("Num", 2);
    list.clear();
    list.append(D_TARGET_CURR_IDX);
    list.append(Q_TARGET_CURR_IDX);
    ui->checkBox_dq_curr_target->setProperty("Id", list);

    ui->checkBox_dq_vol->setProperty("Num", 2);
    list.clear();
    list.append(D_AXIS_VOL_IDX);
    list.append(Q_AXIS_VOL_IDX);
    ui->checkBox_dq_vol->setProperty("Id", list);

    ui->checkBox_svpwm->setProperty("Num", 3);
    list.clear();
    list.append(SVPWM_DUTYA_IDX);
    list.append(SVPWM_DUTYB_IDX);
    list.append(SVPWM_DUTYC_IDX);
    ui->checkBox_svpwm->setProperty("Id", list);

    ui->checkBox_dc_curr->setProperty("Num", 1);
    ui->checkBox_dc_curr->setProperty("Id", DC_CURRENT_IDX);

    //ui->checkBox_angle_diff->setProperty("Num", 1);
    //ui->checkBox_angle_diff->setProperty("Id", AGNLE_DIFF_IDX);
}

bool MainWindow::CheckSelectedBox(QCheckBox *box, bool checked) {
    QList<QCheckBox *> children = ui->frame_16->findChildren<QCheckBox *>();
    int num = 0;
    for(int i = 0; i < children.size(); i++) {
        if (box != children[i] && children[i]->isChecked()){
            num += children[i]->property("Num").toInt();
        }
    }
    int id_num = box->property("Num").toInt();
    if (num + id_num > MAX_SELECT_BOX && checked) {
        box->setCheckState(Qt::Unchecked);
        return false;
    }
    QList<int> list;
    if (id_num == 1) {
        int id = box->property("Id").toInt();
        list.append(id);
    }else {
        QList<QVariant> ids = box->property("Id").toList();
        for (int i = 0; i < ids.size(); i++) {
            list.append(ids.at(i).toInt());
        }
    }
    for (int i = 0; i < list.size(); i++) {
        if (checked) {
            plotMan->AddGraph(list.at(i));
        }else {
            plotMan->removeGraph(list.at(i));
        }
    }
    ui->plot->replot();
    mc->setPoltIds(plotMan->getIds());
    return true;
}
